package com.banciyuan.bcywebview.biz.main.group.talk;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.GroupWork;
import de.greenrobot.daoexample.model.TagDetail;

public class GroupTalkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<GroupWork> glist;
    private Context mContext;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    public String joinFormat;
    public String talkGroupFormat;
    public String joinGroupFormat;

    public GroupTalkAdapter(List<GroupWork> glist, Context mContext) {
        this.glist = glist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.imageLoader = ImageLoader.getInstance();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new GroupPicViewHolder(inflater.inflate(R.layout.group_main_item, parent, false));
            case 1:
                return new GroupNopicViewHolder(inflater.inflate(R.layout.group_main_item_without_pic, parent, false));
            default:
                return new GroupNopicViewHolder(inflater.inflate(R.layout.group_main_item_without_pic, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                setGroupPicData(position, (GroupPicViewHolder) holder);
                break;
            case 1:
                setGroupNoPicData(position, (GroupNopicViewHolder) holder);
                break;
        }
    }

    private void setGroupNoPicData(int position, GroupNopicViewHolder viewGroupBase) {
        final GroupWork team = glist.get(position);
        renderGroupTagData(mContext, viewGroupBase.tag_text, viewGroupBase.workForm, team);
        setPostNum(viewGroupBase.post_num, team);
        if (!TextUtils.isEmpty(team.getName())) {
            viewGroupBase.title.setText(Html.fromHtml(team.getName()));
        }
        if (team.getPosts() != null && !team.getPosts().isEmpty()) {
            viewGroupBase.bestPost.setVisibility(View.VISIBLE);
            viewGroupBase.bestPost.setText(Html.fromHtml(team.getPosts().get(0).getPlain()));
        } else {
            viewGroupBase.bestPost.setVisibility(View.GONE);
        }
        ((View) (viewGroupBase.title.getParent())).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, team.getGid(), team.getName());
            }
        });
    }

    private void setPostNum(TextView tvPostNum, GroupWork team) {
        if (!TextUtils.isEmpty(team.getPost_num())) {
            int postNum = Integer.parseInt(team.getPost_num());
            if (postNum > 10) {
                tvPostNum.setText(String.format(talkGroupFormat, team.getPost_num()));
            } else {
                tvPostNum.setText(joinFormat);
            }
        } else {
            tvPostNum.setText(joinFormat);
        }
    }

    private void setGroupPicData(int position, GroupPicViewHolder viewGroupPic) {
        final GroupWork team = glist.get(position);
        renderGroupTagData(mContext, viewGroupPic.tag_text, viewGroupPic.workForm, team);
        setPostNum(viewGroupPic.post_num, team);
        if (!TextUtils.isEmpty(team.getName())) {
            viewGroupPic.title.setText(Html.fromHtml(team.getName()));
        }
        if (team.getPosts() != null && !team.getPosts().isEmpty() && team.getPosts().get(0).getMulti() != null && !team.getPosts().get(0).getMulti().isEmpty()) {
            viewGroupPic.bestPost.setVisibility(View.VISIBLE);
            viewGroupPic.bestPost.setText(Html.fromHtml(team.getPosts().get(0).getPlain()));
            if (!TextUtils.isEmpty(team.getPosts().get(0).getMulti().get(0).getPath())) {
                viewGroupPic.bestIv.setVisibility(View.VISIBLE);
                imageLoader.displayImage(team.getPosts().get(0).getMulti().get(0).getPath(), viewGroupPic.bestIv, ImageOptions.options);
            } else {
                viewGroupPic.bestIv.setVisibility(View.GONE);
            }
        } else {
            viewGroupPic.bestPost.setVisibility(View.GONE);
            viewGroupPic.bestIv.setVisibility(View.GONE);
        }
        ((View) (viewGroupPic.bestIv.getParent())).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, team.getGid(), team.getName());
            }
        });
    }

    public void renderGroupTagData(final Context mContext, TextView tag_text, View workForm, final GroupWork team) {
        if (!team.getTags().isEmpty()) {
            tag_text.setVisibility(View.VISIBLE);
            workForm.setVisibility(View.VISIBLE);
            final TagDetail t = team.getTags().get(0);
            tag_text.setText(t.getTag_name());
            if (!TextUtils.isEmpty(t.getWork()) && !isLegalWork(t.getWid())) {
                tag_text.setTextColor(mContext.getResources().getColor(R.color.my_like_name));
            } else {
                tag_text.setTextColor(mContext.getResources().getColor(R.color.timeline_green));
            }
            ((View) (tag_text.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(t.getWork())) {
                        if (isLegalWork(t.getWid())) {
                            Intent intent = new Intent(mContext, CircleSmoothActivity.class);
                            intent.putExtra(gotoUtil.INTENT_VALUE_TWO, team.getWid());
                            intent.putExtra(gotoUtil.INTENT_VALUE_ONE, team.getWork());
                            intent.putExtra("from", 2);
                            mContext.startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent(mContext, CircleTagSmoothActivity.class);
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getTag_name());
                        intent.putExtra("type", "tag");
                        intent.putExtra("from", 2);
                        mContext.startActivity(intent);
                    }
                }
            });
            talkGroupFormat = mContext.getString(R.string.person_talk_group_unit);
            joinGroupFormat = mContext.getString(R.string.person_join_group_unit);
            joinFormat = mContext.getString(R.string.wait_for_you_join_n);
        } else {
            tag_text.setVisibility(View.GONE);
            workForm.setVisibility(View.GONE);
            talkGroupFormat = mContext.getString(R.string.person_talk_group_unit_2);
            joinGroupFormat = mContext.getString(R.string.person_join_group_unit_2);
            joinFormat = mContext.getString(R.string.wait_for_you_join);
        }
    }

    private boolean isLegalWork(String wid) {
        try {
            if (TextUtils.isEmpty(wid)|| "false".equals(wid) || Integer.valueOf(wid) <= 0)
                return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public int getItemCount() {
        return glist.size();
    }

    @Override
    public int getItemViewType(int position) {
        GroupWork groupWork = glist.get(position);
        if (groupWork.getPosts() != null
                && !groupWork.getPosts().isEmpty()
                && !groupWork.getPosts().get(0).getMulti().isEmpty()) {
            return 0;
        } else {
            return 1;
        }
    }

    class GroupPicViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView bestPost;
        TextView post_num;
        TextView tag_text;
        ImageView bestIv;
        View workForm;

        GroupPicViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.group_new_item_title_one);
            post_num = (TextView) itemView.findViewById(R.id.tv_postnum);
            bestPost = (TextView) itemView.findViewById(R.id.talk_intro);
            tag_text = (TextView) itemView.findViewById(R.id.tag_text);
            bestIv = (ImageView) itemView.findViewById(R.id.best_img);
            workForm = itemView.findViewById(R.id.from_mark);
        }
    }

    class GroupNopicViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView bestPost;
        TextView post_num;
        TextView tag_text;
        View workForm;

        GroupNopicViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.group_new_item_title_one);
            post_num = (TextView) itemView.findViewById(R.id.tv_postnum);
            bestPost = (TextView) itemView.findViewById(R.id.talk_intro);
            tag_text = (TextView) itemView.findViewById(R.id.tag_text);
            workForm = itemView.findViewById(R.id.from_mark);
        }
    }
}
